package 二叉树;

import java.util.*;

/**
 * 二叉树层次遍历
 * User: zhaixiaolong Date:2024/2/22 9:15 ProjectName: algorithm Version:
 */
public class 二叉树层次遍历 {

    static TreeNode initTree() {
        TreeNode root = new TreeNode(1, new TreeNode(3, new TreeNode(5), new TreeNode(3)), new TreeNode(2, new TreeNode(4), new TreeNode(9)));
        return root;
    }

    public static void main(String[] args) {
        TreeNode treeNode = initTree();
        List<Integer> levelMaxValueList = levelMaxValue(treeNode);
        System.out.println(Arrays.toString(levelMaxValueList.toArray()));
    }

    /**
     * 二叉树广度优先遍历：层次遍历
     *
     * @param root
     * @return
     */
    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            List<Integer> levelTreeNodeList = new ArrayList<>();
            int levelSize = queue.size();
            for (int i = 0; i < levelSize; i++) {
                TreeNode node = queue.poll();
                assert node != null;
                levelTreeNodeList.add(node.getVal());
                if (node.getLeft() != null) {
                    queue.add(node.getLeft());
                }
                if (node.getRight() != null) {
                    queue.add(node.getRight());
                }
            }
            list.add(levelTreeNodeList);
        }
        return list;
    }


    /**
     * 您需要在二叉树的每一行中找到最大的值。
     * 在每个树行中找最大值 (https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/)
     *
     * @param root
     * @return
     */
    public static List<Integer> levelMaxValue(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            int maxValue = Integer.MIN_VALUE;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                maxValue = Math.max(maxValue, node.getVal());
                if (node.getLeft() != null) {
                    queue.add(node.getLeft());
                }
                if (node.getRight() != null) {
                    queue.add(node.getRight());
                }
            }
            list.add(maxValue);
        }
        return list;

    }

    /**
     * 填充每个节点的下一个右侧节点指针
     * 填充每个节点的下一个右侧节点指针 (https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/)
     * 给定一个 完美二叉树 ，其所有叶子节点都在同一层，每个父节点都有两个子节点。
     * 填充它的每个 next 指针，让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点，则将 next 指针设置为 NULL
     * 初始状态下，所有 next 指针都被设置为 NULL
     *
     * @param root
     */
    public static void initNextRightPoint(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<>();
        while (!queue.isEmpty()) {
            TreeNode pre = null;
            int queueSize = queue.size();
            for (int i = 0; i < queueSize; i++) {
                TreeNode node = queue.poll();
                if (i == 0){
                    pre = node;
                }else {
                    pre.next = node;
                    pre = node;
                }
                //左孩子
                if (node.left != null) {
                    queue.offer(node.left);
                }
                //右孩子
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
        }

    }


}
